package com.net.xpay.core.validator;

import com.google.common.base.Preconditions;
import com.net.xpay.common.domain.User;
import com.net.common.exception.BusinessException;
import com.net.common.util.MoneyUtil;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.enums.BillPayMethod;
import com.net.xpay.core.model.BuyModel;
import com.net.xpay.core.model.ChargeModel;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author  on 24/04/2018.
 * 验证PayModel的一些逻辑
 * @see BuyModel
 */
@Component
public class BuyModelValidator {

    public void ensurePayParameter(User user, ChargeModel buyModel) {
        if (buyModel.getAmount() <= 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付金额必须大于0");
        }
        if (buyModel.getBillPayMethod() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付方式不能为空");
        }
        if (buyModel.getAccountMoney() < 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "账户付款金额不能小于0");
        }
        if (buyModel.getPayChannelMoney() < 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "第三方支付金额不能小于0");
        }

        if (buyModel.getAccountMoney() + buyModel.getPayChannelMoney() != buyModel.getAmount()) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "账户支付和第三方支付之和必须等于支付金额");
        }

        if (MoneyUtil.convertCentToBigDecimal(buyModel.getAmount(), 2).compareTo(new BigDecimal("1")) < 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "单笔充值金额不能低于1元");
        }

        if (buyModel.getBillPayMethod() == BillPayMethod.ALIPAY_APP_QRCODE) {
            //普通扫描付款 当面付
            if (MoneyUtil.convertCentToBigDecimal(buyModel.getAmount(), 2).compareTo(new BigDecimal("20000")) > 0) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "扫码支付单笔充值金额不能超过20000元");
            }
        } else if (buyModel.getBillPayMethod() == BillPayMethod.ALIPAY_WEB_PAGE) {
            //电脑网站支付
            if (MoneyUtil.convertCentToBigDecimal(buyModel.getAmount(), 2).compareTo(new BigDecimal("100000")) > 0) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "电脑端充值单笔充值金额不能超过100000元");
            }
        } else {
            //签约付款
            if (MoneyUtil.convertCentToBigDecimal(buyModel.getAmount(), 2).compareTo(new BigDecimal("20000")) > 0) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "转账充值单笔充值金额不能超过20000元");
            }
        }

        validateBillPayMethod(buyModel);
    }

    private void validateBillPayMethod(BuyModel buyModel) {
        if (buyModel.getAccountMoney() != 0 && buyModel.getPayChannelMoney() == 0) {
            Preconditions.checkArgument(buyModel.getBillPayMethod() == BillPayMethod.ALL_ACCOUNT, "billPayMethod不正确");
        } else if (buyModel.getAccountMoney() != 0 && buyModel.getPayChannelMoney() != 0) {
            Preconditions.checkArgument(buyModel.getBillPayMethod() == BillPayMethod.ACCOUNT_AND_PAY_CHANNEL, "billPayMethod不正确");
        } else if (buyModel.getAccountMoney() == 0 && buyModel.getPayChannelMoney() == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "总金额必须大于0");
        }
    }
}

